﻿<?php
include 'test_header.php';

class test_user_sign_process extends UnitTestCase{
	
	function setUp() {
		$db = db_connect();
		$sql = "
CREATE TABLE IF NOT EXISTS `user` (
  `username` varchar(255) NOT NULL,
  `passwd` varchar(128) NOT NULL,
  `role` varchar(16) NOT NULL,
  `email` varchar(128) NOT NULL,
  `signup_time` varchar(64) DEFAULT NULL,
  `last_signin_time` varchar(64) DEFAULT NULL,
  `last_signin_ipadd` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
		$res = $db->query( $sql );
	
		$sql ="
CREATE TABLE IF NOT EXISTS `user_profile` (
  `username` varchar(255) NOT NULL,
  `nick` varchar(255) DEFAULT NULL,
  `sex` varchar(6) DEFAULT NULL,
  `location` varchar(1024) DEFAULT NULL,
  `motto` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
		$res = $db->query( $sql );

		$sql = "
INSERT INTO `user` (`username`, `passwd`, `role`, `email`, `signup_time`, `last_signin_time`, `last_signin_ipadd`) VALUES
('test', '202cb962ac59075b964b07152d234b70', '0', '', '2010-05-08', NULL, NULL)
";
		$res = $db->query( $sql );
	}
	function tearDown(){ 
		
		$db = db_connect();
		$sql = 'DROP TABLE `user`';
		$res = $db->query( $sql );
		$sql = 'DROP TABLE `user_profile`' ;
		$res = $db->query( $sql );
	}

	function test_class_user_sign_process_function_do_singin_success(){
		
		$db = db_connect();
		//使用在setUp函数中插入的记录,断言登录成功
		$request = array( 'username'=>'test' , 'passwd'=>'123' );
		
		$user_sign_process = new user_sign_process( $db , $request );
		
		$this->assertTrue( $user_sign_process->do_signin() );

		//断言会话变量中已经有相应的记录
		$this->assertTrue( !empty($_SESSION['username']) );
		//退出登录,以免影响其他测试
		$this->assertTrue( $user_sign_process->do_signout() );
	}

	function test_user_sign_process_function_do_signout(){
		
		$db = db_connect();
		$request = array( 'username'=>'test' , 'passwd'=>'123');

		$user_sign_process = new user_sign_process( $db , $request );
		
		$user_sign_process->do_signin();		
		$this->assertTrue( $user_sign_process->do_signout() );

		//断言会话变量中的相应记录为空
		$this->assertTrue( !isset($_SESSION['username'])||empty($_SESSION['username']) );            
	}

	function test_user_sign_process_function_do_signin_failure(){
		
		$db = db_connect() ;
		//使用数据库中不存在的记录,断言登录失败
		$request = array( 'username'=>'ffff' , 'passwd'=>'lll');
		
		$user_sign_process = new user_sign_process( $db , $request );
		
		$this->assertFalse( $user_sign_process->do_signin() );
		$this->assertTrue( !isset($_SESSION['username'])||empty($_SESSION['username']) );
	
	}	


}

$test = new test_user_sign_process();
$test->run( new HtmlReporter() );

